/**
 * 路由相关帮助函数
 *
 * @author 俞杰
 * @email 158392613@qq.com
 */
import { MenuItem, MenuLevel } from '@/api/userCenter/system/model/menuModel'
import { cloneDeep } from 'lodash-es'
import { Router, RouteRecordRaw } from 'vue-router'
import { ApiResponse } from './service/request'
// 修复打包后找不到动态路由下组件的问题
const modules = import.meta.glob('../views/**/*.{vue,tsx}')
/**
 * 递归生成路由
 * @param menuList 原始APi返回的路由信息
 * @param routeList tempRoute
 * @returns routeRes
 */
export const generateRoute = (menuList: MenuItem[]) => {
  const routeList: RouteRecordRaw[] = []
  for (let i = 0; i < menuList.length; i++) {
    const menuItem = menuList[i]
    if (
      (menuItem.level == MenuLevel.MENU || menuItem.level == MenuLevel.PAGE) &&
      menuItem.children &&
      menuItem.children.length > 0 &&
      (menuItem.children[0].level == MenuLevel.MENU ||
        menuItem.children[0].level == MenuLevel.PAGE)
    ) {
      const routeItem: RouteRecordRaw = {
        path: menuItem.path,
        name: menuItem.code,
        meta: {
          title: menuItem.name,
        },
        component: modules[`../views${menuItem.path}/index.vue`],
        children: cloneDeep(generateRoute(menuItem.children)),
      }
      routeList.push(routeItem)
    } else if (
      menuItem.level == MenuLevel.MENU ||
      menuItem.level == MenuLevel.PAGE
    ) {
      const routeItem: RouteRecordRaw = {
        path: menuItem.path,
        name: menuItem.code,
        meta: {
          title: menuItem.name,
        },
        component: modules[`../views${menuItem.path}/index.vue`],
      }
      routeList.push(routeItem)
    }
  }
  return routeList
}

/**
 * 拍平树结构的菜单数据
 *
 * @param menuList 当前树结构的菜单数据
 */
export const getFlatMenuList = (
  menuList: MenuItem[],
  list: { [key: string]: MenuItem } = {}
) => {
  for (let i = 0; i < menuList.length; i++) {
    const menu = menuList[i]
    list[menu.id] = menu
    if (menu.children && menu.children.length > 0) {
      getFlatMenuList(menu.children, list)
    }
  }
  return list
}

/**
 * 安装接口返回的路由
 *
 * @param router router实例
 * @param parentName 父路由名[name]
 * @param menuList 接口返回的路由记录
 */
export const installApiRoute = (
  router: Router,
  parentName: string,
  menuList: MenuItem[]
) => {
  const routeList = generateRoute(menuList)
  routeList.forEach((item) => {
    router.addRoute(parentName, item)
  })
}

type FilterPaiXinSysRouterInfoFun = (
  apiResp: ApiResponse<MenuItem[]>,
  appId?: string
) => ApiResponse<MenuItem[]>
/**
 * paixin-admin appid
 */
const PAIXIN_ADMIN_APP = '1416227462637613058'
/**
 * 过滤出指定应用id 的菜单
 * @param apiResp
 * @returns
 */
export const filterPaiXinSysRouterInfo: FilterPaiXinSysRouterInfoFun = (
  apiResp,
  appId = PAIXIN_ADMIN_APP
) => {
  const data = apiResp.data.find((item) => item.id === appId)?.children || []
  return {
    ...apiResp,
    data,
  }
}

export const getButtonPerm = (
  menuList?: MenuItem[],
  actionList: string[] = []
): string[] => {
  if (!menuList) {
    return []
  }
  for (let i = 0; i < menuList.length; i++) {
    const menu = menuList[i]
    if (menu.level === MenuLevel.BUTTON) {
      actionList.push(menu.code)
    } else if (menu.children && menu.children.length > 0) {
      getButtonPerm(menu.children, actionList)
    }
  }
  return actionList
}

export interface MenuItemWithoutIcon extends Omit<MenuItem, 'icon'> {
  children?: MenuItemWithoutIcon[]
}
export function deleteIcon(
  menuList: MenuItem[],
  res: MenuItemWithoutIcon[] = []
): MenuItemWithoutIcon[] {
  for (let i = 0; i < menuList.length; i++) {
    const menu = menuList[i]
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    const { icon, ...resMenuItem } = menu
    res.push(resMenuItem)
    if (menu.children && menu.children.length > 0) {
      deleteIcon(menu.children, res)
    }
  }
  return res
}
